package com.adou.d10.reflect;

import org.junit.Test;

import java.lang.reflect.Method;

public class Testmethod {
    @Test
    public void testGetMethods() throws Exception {
        // 1.得到Class对象
        Class c = Cat.class;

        // 2.获取所有方法
        Method[] declaredMethods = c.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod.getName() + " - " + declaredMethod.getParameterCount() + " - " + declaredMethod.getReturnType());
        }

        System.out.println("//////////////////////////////////-");
        // 3.获取某个方法对象
        Method run = c.getDeclaredMethod("run");
        System.out.println(run.getName() + " - " + run.getParameterCount() + " - " + run.getReturnType());

        // 执行
        Cat cat = new Cat();
        run.setAccessible(true);
        Object rs = run.invoke(cat);//调用无参run方法
        System.out.println(rs); // null


        System.out.println("//////////////////////////////////-");

        Method eat = c.getDeclaredMethod("eat", String.class);
        System.out.println(eat.getName() + " - " + eat.getParameterCount() + " - " + eat.getReturnType());

        eat.setAccessible(true);
        String rs2 = (String)eat.invoke(cat, "咖啡猫");//调用无参run方法
        System.out.println(rs2); // null


    }
}
